package es.bracana.rep;

import java.util.List;


import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import es.bracana.domain.Clients;

@Repository
@Transactional
public class ClientsDAOImpl implements ClientsDAO {

    @Autowired
    private EntityManager em;
	
	public Clients findById(String id) {
	     return em.find(Clients.class, id);
	}

	public List<Clients> findAllOrderedByName() {
	      CriteriaBuilder cb = em.getCriteriaBuilder();
	        CriteriaQuery<Clients> criteria = cb.createQuery(Clients.class);
	        Root<Clients> client = criteria.from(Clients.class);

	        criteria.select(client).orderBy(cb.asc(client.get("Nombre")));
	        return em.createQuery(criteria).getResultList();
	}

	public void register(Clients client) {
		em.persist(client);
		return;
		
	}

}
